package com.chencong.transform;

import com.chencong.env.FlinkTableEnv;
import org.apache.flink.api.common.functions.RichMapFunction;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

/**
 * @Author chencong
 * @Description map富函数
 * @Date 7:58 下午 2021/8/17
 * @Param
 **/
public class MapRichFunc {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment streamTableEnvironment = FlinkTableEnv.getStreamTableEnvironment();
        streamTableEnvironment.setParallelism(1);

        DataStreamSource<Integer> DS = streamTableEnvironment.fromElements(1, 2, 3, 4, 5);

        // TODO RichMapFunction 富函数
        // 1.生命周期方法：open、close =》可以用于 外部连接的 管理
        // 2.运行时上下文：RuntimeContext =》 可以获取 环境信息、状态....

        SingleOutputStreamOperator<String> mapRich = DS.map(new RichMapFunction<Integer, String>() {
            @Override
            public void open(Configuration parameters) throws Exception {
                super.open(parameters);
                System.out.println("open.....");
            }

            @Override
            public String map(Integer value) throws Exception {
                return value + "=======" + getRuntimeContext().getTaskName();
            }

            @Override
            public void close() throws Exception {
                super.close();
                System.out.println("close......");

            }
        });

        mapRich.print();

        streamTableEnvironment.execute();

    }
}
